- /* sdmput.cpp by K.Tsuru */
- // function ID = 344 DRADIX
- /*****************************************************************
- SDouble class
- It provides the output.
- In order to decrease the code size, no function of iostream class
- is used.
- In header "sdbl.h" the function prototype is given below
- --------
- long Put(long figUnit=5, long pf = 0, int pL=0, int lF = CRLF|ROUND|INT_PUT, int dm=' ') const;
- long Puts(long figUnit=5,long pf = 0, int pL=0, int lF = CRLF|ROUND|INT_PUT, int dm=' ') const;
- --------
- output format of SDouble
-
- 1.234567890 1234567890 1234567890 1234567890 1234567890 ( 50)
- <- fU=10 ->
- <---------------- perLine = 5 units -----------------> (SHOW_FIG)
-
- 1) If figUnit == 0, it continuously outputs with neither delimiter nor carridge return.
- 2) putFig : the number of decimal figures. default : putFig = 0 all figures
- 3) perLine = the number of blocks which contains "fig" decimal per line
- default : perLine = 0 automatically set perLine = displayWidth/(fig+1)
- 4) lF(line format) : NO_CR, CRLF, CONTINUE and END_CR have same meaning as SLong class's ones.
- ROUND : do Round() or not.
- INT_PUT : It outputs a decimal figure as integer part.
- SHOW_FIG: It shows the number of decimal figures at end of line and adds carridge return.
- 5) Insert delimiter given by "delmt" at intervals of "fig(Unit)" characters.
- If delmt == 0, no delimiter is inserted.
- It returns the total number of output bytes, including '\n', '.' and delimiter,
- as "printf()".
-
- ******************************************************************/
- #ifndef SN_H
- #include "sn.h"
- #endif
-
- static int putcr(int cr, FILE* out){
- if(cr) putc('\n', out);
- return cr ? 1 : 0;
- }
- static int putdelmt(int d, FILE* out){
- if(d) putc(d, out);
- return d ? 1 : 0;
- }
-
- /**************************************************
- sub-function
- It returns (n+1)-th decimal place by a character.
- The mantissa begins at figure[1].
- ***************************************************/
- // function ID = 306
- int SDouble::CharNthDec(long n) const{
- static int pos = -1;
- static char buff[DFIGURES+1];
- int p, r;
-
- if(n < 0){ pos = -1; return 0; } //initialize
-
- p = int(n/DFIGURES) +1; //in figure[p]
- r = int(n%DFIGURES); //r-th position in figure[p]
-
- if(p != pos){ //renew buff[]
- pos = p;
- fType f = figure(p);
- for(int j = DFIGURES-1; j >= 0; j--){
- buff[j] = '0' + f%10;
- f /= 10;
- }
- }
- return (int)buff[r];
- }
-
- // function ID = 344
- /*********************************************************
- main body
- usage : x.Put()
- putFig : the number of output figures
- fig : distance of delimiter
- delmt : delimiter, default is a space ' '
- It returns the total number of bites of output characters.
- **********************************************************/
- //long SDouble::Put(long fig, long putFig, int perLine, int lF, int delmt) const{
- long SDouble::Put(long fU, long pF, int pL, int lF, int dm) const{
- SignCheck(344); // sign == UNDECIDED ?
- FILE* out = FileStream();
-
- if(fU > 0) {
- figUnit = fU; putFig = pF; perLine = pL; lineFormat = lF; delmt = dm;
- } else if(fU == 0){ putFig = LONG_MAX; delmt = 0; } // continuously output with no delimiter
- // else uses current format
- int showFig = lineFormat & SHOW_FIG;
- int crlf = lineFormat & END_CR;
- if( Sign(344) == 0 ){
- putc('0',out); putc('.',out); putc('0',out); return 3+putcr(crlf, out);
- }
-
- SDouble temp(*this);
-
- long cCount = 0;
- char buff[displayWidth+1];
- int nc = 0; //the number of characters in a line
- long decCount = 0; //0.abcd|efgh|.... counter of decimal part
-
- temp.StdReform(344); //standard form
-
- // lF temp display initial value of decCount
- // lF & INT_PUT 0|000a|bcde|fgh..... a.bcdefgh... DFIGURES
- // else 0|abcd|efgh|..... 0.abcdefgh.... 0
- uint last; //last position of output
-
- if(lineFormat & ROUND) temp.Round(); //round off
-
- long exp = DFIGURES*(long)temp.RdxExp();//exponent in a decimal system
- ulong v = (ulong)temp.figure(1);
- #ifndef NDEBUG
- assert(v);
- #endif
- //It reforms into "0.abcd ...".
- int k = 0;
- while(v < DRADIX){ v *= 10; k++; }
- k--;
- temp.TempPointFree(); //temporally into floating point out is changed ?
- if(k > 0) {
- v = (ulong)ipow10(k); temp = DsMult(temp, v); exp -= k;
- }
-
- if(lineFormat & INT_PUT){
- //In order to display such as "a.bcdef...." it multiplies by ten.
- if(putFig > 1) putFig--;
- temp = DsMult(temp, 10); exp--;
- decCount = DFIGURES;
- }
- temp.PointModePop();
-
- if(lineFormat & ROUND) last = min(temp.Last(), temp.EffFig()); //do not display hidden figures
- else last = temp.Last();
-
- //the number of decimal figures between decCount/DFIGURES and last
- long decfig = long(last - decCount/DFIGURES)*(long)DFIGURES; //maybe last=1
- if(putFig > 0) decfig = min(decfig, putFig);
- //display the negative sign
- if( temp.Sign(344) == -1 ){ putc('-', out); cCount++; }
- //display the integral part
- buff[0] = '0'; buff[1] = '\0'; // buff[] = "0"
- if(decCount) buff[0] += temp.figure(1);
-
- if( (decfig + decfig/figUnit) > (int)displayWidth ){
- strcat(buff, ".+");
- if(lineFormat & CONTINUE) strcat(buff, "\\");
- cCount += fprintf(out, "%s\n",buff); //over one line
- } else {
- cCount += fprintf(out, "%s.",buff); //show point
- nc = (int)cCount;
- }
- //decimal part
- int rest; //rest of characters at last line
- int figWidth; //the width of figure display at the end of line, except ()
- int dec_init = (int)decCount;
-
- if(!decfig){ //decimal part=0
- putc('0', out); cCount++;
- rest = displayWidth - (int)cCount;
- } else {
- //non zero decimal part
- int putdec = 0, crLine, figCount = 0;
- figWidth = iFigures(decfig);
- crLine = (perLine > 0 ) ? perLine : int( (displayWidth-1)/(figUnit+1) );
- if(showFig) while((int)displayWidth -crLine*(figUnit+1)<(figWidth+3) ) crLine--;
- if( crLine<=0 ) crLine = 1;
-
- temp.CharNthDec(-1L);
- while(decfig){
- putc( temp.CharNthDec(decCount), out);
- decfig--; cCount++; decCount++; nc++; putdec++;
- if(putdec == figUnit){
- figCount++; nc++; putdec = 0;
- if( figCount != crLine ) cCount += putdelmt(delmt, out);
- }
- if( figCount == crLine ){ //reaches at line end
- if(showFig) cCount += fprintf(out, " (%*ld)", figWidth, decCount - dec_init);
- if(delmt != ' ') cCount += putdelmt(delmt, out);
- if(lineFormat & CONTINUE){ putc('\\', out); cCount++; }
- if(lineFormat & (CRLF|SHOW_FIG)){
- putc('\n', out); cCount++;
- }
- figCount = 0; nc = 0;
- }
- }
- rest = int((figUnit+1)*crLine -nc);
- }
- //display exponent part
- if(exp){
- cCount += sprintf(buff,"e%+ld",exp);
- if(rest < (int)strlen(buff) ){
- if(lineFormat & CONTINUE){ putc('\\', out); cCount++; }
- putc('\n', out); cCount++; rest = displayWidth;
- }
- rest -= fprintf(out, "%s", buff);
- }
-
- //display the number of figures at the last line
- if(showFig && nc && (decCount - dec_init) ){//When nc=0 the display ended.
- sprintf(buff,"(%ld)", decCount - dec_init);
- cCount += fprintf(out, "%*s%s",rest," ", buff);
- }
- cCount += putcr(crlf, out);
- return cCount;
- }
-
sdmput.cpp : last modifiled at 2017/08/21 12:10:30(7,197 bytes)
created at 2017/10/07 10:21:15
The creation time of this html file is 2017/10/07 10:30:03 (Sat Oct 07 10:30:03 2017).